package com.ytd.smartcanteen.weighting.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.ytd.smartcanteen.weighting.MainActivity;
import com.ytd.smartcanteen.weighting.util.FoodManager;

import cn.ytd.smartcanteen.weighting.R;

public class SetDialog extends BaseDialog {


    private final View mMenuView;

    private int clickCount;


    @SuppressLint("InflateParams")
    public SetDialog(MainActivity context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_set, null);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        TextView btn_back = (TextView) mMenuView.findViewById(R.id.set_btn_back);
        TextView btn_set = (TextView) mMenuView.findViewById(R.id.set_btn_set);
        TextView btn_synchronization = (TextView) mMenuView.findViewById(R.id.set_btn_synchronization);
        TextView btn_bind = (TextView) mMenuView.findViewById(R.id.set_btn_bind);

        btn_set.setOnClickListener(v -> context.showSetDishDialog());
        btn_synchronization.setOnClickListener(v -> {
            context.showProgressDialog(context.getString(com.ytd.common.R.string.base_downloading_data), false);
            FoodManager.getInstance().initFoodData();
            FoodManager.getInstance().initBindFood();
        });
        btn_bind.setOnClickListener(v -> context.showBindingDialog());

        btn_back.setOnClickListener(v -> {
            //销毁弹出框
            dismiss();
        });

        mMenuView.findViewById(R.id.ll_port).setOnClickListener(v -> {
            v.removeCallbacks(mRunnable);
            v.postDelayed(mRunnable, 3000);
            clickCount++;
            if (clickCount == 5) {
                context.showPortDialog();
            }
        });

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(h - 50);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.DialogShow);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        context.setTitle(context.getString(com.ytd.common.R.string.base_dialog_btn_setting));
    }

    private final Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            clickCount = 0;
        }
    };

    public void Show(int gravity, int x, int y) {
        showAtLocation(mMenuView, gravity, x, y);
    }

}
